// @Author miaoqing
// @Date 2024/03/28 18:25:00
// @Desc
package logic

import (
	"core/component/logger"
	"sync"
)

var (
	cMgr     *ClientManager
	cMgrOnce sync.Once
)

type ClientManager struct {
	clients    *sync.Map
	bindUserID *sync.Map
}

func clientMgrGetMe() *ClientManager {
	cMgrOnce.Do(func() {
		cMgr = &ClientManager{
			clients:    &sync.Map{},
			bindUserID: &sync.Map{},
		}
	})
	return cMgr
}

func (mgr *ClientManager) addClients(clientID int64, c *Client) {
	if _, ok := mgr.clients.Load(clientID); ok {
		return
	}
	mgr.clients.Store(clientID, c)
	logger.Infof("addFrameClient clientID:%v", clientID)
}

func (mgr *ClientManager) getBindUserID2Client(userID uint64) *Client {
	val, ok := mgr.bindUserID.Load(userID)
	if !ok {
		return nil
	}
	return val.(*Client)
}

func (mgr *ClientManager) sendMsgToClient(userID uint64, data []byte) bool {
	client := mgr.getBindUserID2Client(userID)
	if client == nil {
		return false
	}
	return client.AsyncSendRowMsg(data)
}
